package com.leyou.item.controller;

import com.leyou.common.utils.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> findBrandByPageAndSort(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc",  defaultValue = "false") Boolean desc,
            @RequestParam(value = "key", required = false) String key) {
        PageResult<Brand> result = brandService.findBrandByPageAndSort(page,rows,sortBy,desc,key);
        if (result == null || result.getItems().size() == 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(result);

    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> savebrand(Brand brand ,@RequestParam("cids") List<Long> cids){
        this.brandService.savebarnd(brand,cids);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> findBrandByCid(
            @PathVariable(value = "cid") Long cid){
        List<Brand> list = brandService.findBrandByCid(cid);
        if (list == null || list.size()< 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);

    }
    /**
     * @author:zty
     * @date: 2019年01月16日 17:06:32
     * @description 根据品牌id查询品牌
     * @method  queryBrandByBid
     * @param bid
     * @return org.springframework.http.ResponseEntity<com.leyou.item.pojo.Brand>
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<Brand> queryBrandByBid(@PathVariable("bid")Long bid){
        if (null == bid){
            return ResponseEntity.badRequest().build();
        }
        Brand brand = brandService.queryBrandByBid(bid);
        if (null == brand){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brand);
    }
    /**
     * @author:zty
     * @date: 2019年01月16日 17:13:12
     * @description 根据品牌id查询品牌类别
     * @method  queryBrandByIds
     * @param ids
     * @return org.springframework.http.ResponseEntity<java.util.List<com.leyou.item.pojo.Brand>>
     */
    @GetMapping("list")
    public ResponseEntity<List<Brand>> queryBrandByIds(@RequestParam("ids")List<Long> ids){
        if (CollectionUtils.isEmpty(ids)){
            return ResponseEntity.badRequest().build();
        }
        List<Brand> brands = brandService.queryBrandByIds(ids);
        if (CollectionUtils.isEmpty(brands)){
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.ok(brands);
    }
    /**
     * @author:zty
     * @date: 2019年01月16日 17:30:52
     * @description 通过id查询上品牌名字
     * @method  queryBrandNameByBid
     * @param bid
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     */
    @GetMapping("name/{bid}")
    public ResponseEntity<String> queryBrandNameByBid(@PathVariable("bid")Long bid){
        if (null == bid){
            return ResponseEntity.badRequest().build();
        }
        String brandName = brandService.queryBrandNameByBid(bid);
        if (StringUtils.isEmpty(brandName)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brandName);
    }
    /**
     * @author:zty
     * @date: 2019年01月16日 17:34:32
     * @description 删除品牌
     * @method  delete
     * @param bid
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     */
    @GetMapping("delete/{bid}")
    public ResponseEntity<String> delete(@PathVariable("bid")Long bid){
        if (null == bid){
            return ResponseEntity.badRequest().build();
        }
        int i = brandService.deleteBrand(bid);
        if (i<=0){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }

    @PutMapping("update")
    public ResponseEntity<Void> updatebrand(Brand brand){
        if (null == brand){
            return ResponseEntity.badRequest().build();
        }
        int row = brandService.updateBrand(brand);
        if (row > 0){
            return ResponseEntity.ok().build();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
